﻿using System.ComponentModel;
using Microsoft.AspNetCore.Authorization;
using ZeroSum.App.Datas;
using ZeroSum.App.Extensions;
using ZeroSum.Plugin.Authorization.Datas.Dto.In;
using ZeroSum.Plugin.Authorization.Datas.Dto.Out;
using ZeroSumZSA.Api.Apis;
using ZeroSumZSA.Application.ApplicationServices.Auths;

namespace ZeroSumZSA.Api.Controllers;

/// <summary>
///     授权管理
/// </summary>
[Description("管理-授权管理")]
public class AuthController : ApiController
{
    private readonly IAuthApplicationService _applicationService;

    /// <summary>
    ///     权限
    /// </summary>
    /// <param name="applicationService"></param>
    public AuthController(IAuthApplicationService applicationService)
    {
        _applicationService = applicationService;
    }

    /// <summary>
    ///     获取身份认证Token
    /// </summary>
    /// <param name="dto">TokenDto</param>
    /// <returns>JSON操作结果</returns>
    [HttpPost]
    [AllowAnonymous]
    [Description("Token")]
    public async Task<ApiResponse<TokenDto>> LoginAsync([FromBody] LoginDto dto)
    {
        var tokenDto = await _applicationService.LoginAsync(dto);
        // 设置Swagger自动登录
        Request.HttpContext.SigninToSwagger(tokenDto?.AccessToken);
        // 设置刷新Token令牌
        Request.HttpContext.Response.Headers["x-access-token"] = tokenDto?.RefreshToken;
        return ApiResponse<TokenDto>.Of(tokenDto!);
    }
}